clear all
set more off
set matsize 2000

*input folder path    
global filepath "D:\Dropbox\NREGA Aggregate\shared NREGA lights\replication files"

	  
cd "$filepath"
use "district light panel.dta", clear

local pre "i.year#c.wage i.year#c.outputwage i.year#c.state_frac"


*****************************
*** Fig. 1a: Mean by Wave ***
*****************************
set scheme s1mono

bysort year: egen avglt_06 = mean(avglt) if year>=2000 & nr06==1
bysort year: egen avglt_07 = mean(avglt) if year>=2000 & nr07==1
bysort year: egen avglt_08 = mean(avglt) if year>=2000 & nr08==1

label var avglt_06 "Wave 1"
label var avglt_07 "Wave 2"
label var avglt_08 "Wave 3"

twoway (scatter avglt_06 year) (scatter avglt_07 year) (scatter avglt_08 year), xtitle(Year) ytitle(Avg. Night-time Light Index) ylabel(0(2)10) xlabel(2000(2)2014) xline(2006)


******************************************
*** Fig. 2a: Event Figure, Early Waves ***
******************************************
gen wave1=nr06
gen wave2=nr07
gen wave3=nr08
tab year, gen(yr)

*store wave 1 results
forvalues i=1/14{
gen w`i'=0 if wave1~=.
	replace w`i'=1 if wave1==1 & yr`i'==1
	
gen x`i'=0 if wave2~=.
	replace x`i'=1 if wave2==1 & yr`i'==1
}

*omit 2006
replace w6=0 if wave1~=.
replace x6=0 if wave2~=.


*estimation
reghdfe std_lt w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 w14 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14, absorb(sno year#st) cluster(sno)

*store
estadd ysumm
eststo lights_w1

drop w1-w14 x1-x14


*store wave 2 results
forvalues i=1/14{
gen w`i'=0 if wave2~=.
	replace w`i'=1 if wave2==1 & yr`i'==1
	
gen x`i'=0 if wave1~=.
	replace x`i'=1 if wave1==1 & yr`i'==1
}

*omit 2006
replace w6=0 if wave2~=.
replace x6=0 if wave1~=.


*estimation
reghdfe std_lt  w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 w14 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14, absorb(sno year#st) cluster(sno)

*store
estadd ysumm
eststo lights_w2



*label years
forvalue i=1/14{
local j = 1999+`i'
label var w`i' "`j'"
}


set scheme s1mono
coefplot (lights_w1, label("Wave 1")) (lights_w2, label("Wave 2")), keep(w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 w14) vertical ytitle(Coefficient on Wave*Year)   ylabel(-.5(.1).5) xlabel(,labsize(small)) yline(0) levels(95) ciopts(lpattern(dash)) omitted baselevels ///
     groups(w1 w2 w3 w4 w5 w6 = `""{bf:Pre-Rollout}" "(No Districts)""'        ///
            w7 w8 w9 = `""{bf:Rollout}" "(Early Districts)""'                      ///
            w10 w11 w12 w13 w14    = `""{bf:Post-Rollout}" "(All Districts)""')
			

			
****************************************
*** Fig. 2b: Event Figure, Late Wave ***
****************************************
drop w1-w14 x1-x14

forvalues i=1/14{
gen w`i'=0 if wave3~=.
	replace w`i'=1 if wave3==1 & yr`i'==1
}

*omit 2005
replace w6=0 if wave3~=.


reghdfe std_lt  w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 w14   , absorb(sno year#st) cluster(sno)

estadd ysumm
eststo lights_w3

*label years
forvalue i=1/14{
local j = 1999+`i'
label var w`i' "`j'"
}

*figure output
coefplot (lights_w3, label("Wave 3")) , keep(w*) vertical ytitle(Coefficient on Wave*Year)  ylabel(-.5(.1).5) xlabel(,labsize(small)) yline(0) levels(95) ciopts(lpattern(dash)) omitted baselevels ///
     groups(w1 w2 w3 w4 w5 w6 = `""{bf:Pre-Rollout}" "(No Districts)""'        ///
            w7 w8 w9 = `""{bf:Rollout}" "(Early Dist.)""'                      ///
            w10 w11 w12 w13 w14   = `""{bf:Post-Rollout}" "(All Districts)""') 

			
			
	
	
******************************************************
*** Appendix Figures: Goodman-Bacon Decompositions ***
******************************************************
xtset sno year



*App Fig. 1: GB Decomposition, All Waves
bacondecomp std_lt nregs, ddetail


*App. Fig. 2a: GB Decomposition: 2006 & 2007
preserve 
drop if nr08==1
bacondecomp std_lt nregs, ddetail
restore

**App. Fig. 2b: GB Decomposition: 2007 & 2008
preserve 
drop if nr06==1
bacondecomp std_lt nregs, ddetail
restore

*App. Fig. 2c: GB Decomposition: 2006 & 2008
preserve 
drop if nr07==1
bacondecomp std_lt nregs, ddetail
restore








